package com.cddx.logistics.service.impl;

import com.cddx.logistics.domain.dto.MaterialQueryDTO;
import com.cddx.logistics.domain.pojo.Material;
import com.cddx.logistics.domain.vo.MaterialVO;
import com.cddx.logistics.mapper.MaterialMapper;
import com.cddx.logistics.service.MaterialService;
import com.cddx.logistics.util.model.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class MaterialServiceImpl implements MaterialService {
    @Resource
    private MaterialMapper materialMapper;

    /**
     * 查询分页
     * @param queryDTO
     * @return
     */
    @Override
    public PageInfo<MaterialVO> queryPage(MaterialQueryDTO queryDTO) {
        PageHelper.startPage(queryDTO.getPage(), queryDTO.getLimit());
        List<MaterialVO> list = materialMapper.queryPage(queryDTO);
        return new PageInfo<>(list);
    }



    /**
     * 添加
     * @param material
     * @return
     */
    @Override
    public boolean add(Material material) {
        material.setMaterialCreateTime(new Date());
        material.setMaterialUpdateTime(new Date());
        return materialMapper.add(material) > 0;
    }

    /**
     * 修改
     * @param material
     * @return
     */
    @Override
    public boolean update(Material material) {
        material.setMaterialUpdateTime(new Date());
        return materialMapper.update(material) > 0;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public boolean delete(Long id) {
        return materialMapper.delete(id) > 0;
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    public boolean deleteBatch(String ids) {
        return materialMapper.deleteBatch(ids.split(",")) > 0;
    }

    /**
     * 导入Excel
     * @param file
     * @return
     */
    @Override
    public R importExcel(MultipartFile file) throws Exception {
        // 这里实现Excel导入逻辑，可以使用POI或EasyExcel等工具
        // 示例代码省略具体实现
        return R.error("导入功能暂未实现");
    }

    @Override
    public R getMaterialByInventory(Long storageLocationId) {
        return R.ok(materialMapper.getMaterialByInventory(storageLocationId));
    }

}